package com.stray.crawl.test.forum;

import java.io.InputStream;
import java.text.MessageFormat;
import java.util.List;
import java.util.Properties;

import junit.framework.Assert;

import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Before;

import com.stray.crawl.BeanContext;
import com.stray.crawl.ResultContext;
import com.stray.crawl.utils.IOUtils;
import com.stray.crawl.worker.TaskService;
import com.stray.crawl.worker.task.CrawlTask;

public class AbstractForumTestCase {

	private static final int COLUMN_WIDTH = 30;

	protected TaskService service;

	@Before
	public void setUp() throws Exception {
		service = BeanContext.get(TaskService.class);
	}

	@After
	public void tearDown() throws Exception {
		BeanContext.getContext().close();
	}

	protected long executeTask(CrawlTask task) throws Exception {
		long t = System.currentTimeMillis();

		task.call();

		return System.currentTimeMillis() - t;
	}

	protected String getProperties(String key, Object... args) throws Exception {
		InputStream in = null;

		try {
			Properties p = new Properties();
			in = getClass().getClassLoader().getResourceAsStream("test-data-forums.properties");
			p.load(in);

			String value = p.getProperty(key);
			return MessageFormat.format(value, args);
		} finally {
			IOUtils.closeQuietly(in);
		}
	}

	protected void assertContext(CrawlTask task) {
		ResultContext rctx = task.getResultContext();

		List<Object[]> results = rctx.getResults();
		Assert.assertNotNull(results);

		for (int i = 0; i  < results.size(); i++) {
			Object[] objs = results.get(i);
			boolean recordEmpty = true;

			for (Object obj : objs) {
				if (obj != null && !"".equals(obj.toString())) {
					recordEmpty = false;
					break ;
				}
			}

			if (recordEmpty) {
				Assert.fail("row " + i + " record empty.");
			}
		}
	}

	protected void printTask(CrawlTask task, long time) {
		String[] fields = task.getTaskContext().getFields();
		List<Object[]> results = task.getResultContext().getResults();

		System.out.println();
		System.out.println("Plan Parser Query : " + task.getTaskContext().getExpress());
		int i = 0;
		for (String parser : task.getTaskContext().getQueries()) {
			System.out.println("> " + StringUtils.rightPad(fields[i], 30) + i++ + ";" + parser);
		}

		System.out.println(StringUtils.leftPad("", (COLUMN_WIDTH + 4) * fields.length, "="));

		for (String field : fields) {
			String str = String.format("| %s |", StringUtils.center(field, COLUMN_WIDTH));
			System.out.print(str);
		}

		System.out.println();
		System.out.println(StringUtils.leftPad("", (COLUMN_WIDTH + 4) * fields.length, "="));

		for (Object[] objs : results) {
			for (Object obj : objs) {
				String str = obj.toString();
				str = str.replaceAll("\\n", "");
				str = StringUtils.substring(str, 0, COLUMN_WIDTH);
				str = String.format("| %s |", StringUtils.center(str, COLUMN_WIDTH));
				System.out.print(str);
			}
			System.out.println();
		}

		System.out.println(StringUtils.leftPad("", (COLUMN_WIDTH + 4) * fields.length, "="));

		System.out.println(StringUtils.center("-- Size : " + results.size() + " / Time : " + time + "ms --", (COLUMN_WIDTH + 4) * fields.length));

		System.out.println();
	}


}
